import * as types from './constants'
import genUuid from '../../../utils/uuid'

/**
 * 页面管理
 */

export default {
  state: {
    // 页面字典
    pageMap: new Map(),
    // 页面标题
    title: '',
    // 页面地址
    url: '',
    // 页面图像地址
    img: '',
    // 页面类型
    type: '',
    // 页面UUID
    uuid: '',
    // 当前页面
    currentPage: '',
    // 上个页面
    lastPage: '',
    // 下个页面
    nextPage: '',
    // 窗口宽度
    clientWidth: 0,
    // 左页面信息
    leftPageWidth: 0,
    // 左页面创建
    leftCreated: false,
    // 右页面信息
    rightPageWidth: 0
  },
  actions: {
    updatePageDict ({ commit }, pageDict) {
      commit(types.PAGE_DICT, pageDict)
    },
    genUuid ({ commit }) {
      commit(types.PAGE_UUID)
    },
    getTitle ({ commit }, title) {
      commit(types.PAGE_TITLE, title)
    },
    getUrl ({ commit }, url) {
      commit(types.PAGE_URL, url)
    },
    getImg ({ commit }, img) {
      commit(types.PAGE_IMG, img)
    },
    getType ({ commit }, type) {
      commit(types.PAGE_TYPE, type)
    },
    setCurrentPage ({ commit }, currentPage) {
      commit(types.CURRENT_PAGE, currentPage)
    },
    setLastPage ({ commit }) {
      commit(types.LAST_PAGE)
    },
    setNextPage ({ commit }) {
      commit(types.NEXT_PAGE)
    },
    setClientWidth ({ commit }, clientWidth) {
      commit(types.CLIENT_WIDTH, clientWidth)
    },
    getLeftPageWidth ({ commit }, leftPageWidth) {
      commit(types.LEFT_PAGE_WIDTH, leftPageWidth)
    },
    setIsLeftCreated ({ commit }, leftCreated) {
      commit(types.LEFT_CREATED, leftCreated)
    },
    getRightPageWidth ({ commit }, rightPageWidth) {
      commit(types.RIGHT_PAGE_WIDTH, rightPageWidth)
    }
  },
  mutations: {
    [types.PAGE_DICT] (state, payload) {
      state.pageDict[state.uuid] = payload
    },
    [types.PAGE_UUID] (state) {
      state.uuid = genUuid()
    },
    [types.PAGE_TITLE] (state, payload) {
      document.title = payload
      state.title = payload
    },
    [types.PAGE_URL] (state, payload) {
      state.url = payload
    },
    [types.PAGE_IMG] (state, payload) {
      state.img = payload
    },
    [types.PAGE_TYPE] (state, payload) {
      state.type = payload
    },
    [types.CURRENT_PAGE] (state, payload) {
      state.currentPage = payload
    },
    [types.LAST_PAGE] (state) {
      var currentIndex = state.pageArray.indexOf(state.currentPage)
      if (currentIndex !== 0) {
        state.lastPage = state.pageArray[currentIndex - 1]
      }
      else {
        state.lastPage = null
      }
    },
    [types.NEXT_PAGE] (state) {
      var currentIndex = state.pageArray.indexOf(state.currentPage)
      if (currentIndex !== state.pageArray.length - 1) {
        state.lastPage = state.pageArray[currentIndex + 1]
      }
      else {
        state.nextPage = null
      }
    },
    [types.CLIENT_WIDTH] (state, payload) {
      state.clientWidth = payload
    },
    [types.LEFT_PAGE_WIDTH] (state, payload) {
      state.leftPageWidth = payload
    },
    [types.LEFT_CREATED] (state, payload) {
      state.leftCreated = payload
    },
    [types.RIGHT_PAGE_WIDTH] (state, payload) {
      state.rightPageWidth = payload
    }
  },
  getters: {
    title (state) {
      return state.title
    },
    url (state) {
      return state.url
    },
    img (state) {
      return state.img
    },
    type (state) {
      return state.type
    },
    uuid (state) {
      return state.uuid
    },
    pageArray (state) {
      return state.pageArray
    },
    currentPage (state) {
      return state.currentPage
    },
    lastPage (state) {
      return state.lastPage
    },
    nextPage (state) {
      return state.nextPage
    },
    clientWidth (state) {
      return state.clientWidth
    },
    leftPageWidth (state) {
      return state.leftPageWidth
    },
    leftCreated (state) {
      return state.leftCreated
    },
    rightPageWidth (state) {
      return state.rightPageWidth
    }
  }
}
